//
//  HEAddPhotoCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/18.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEAddPhotoCell.h"
#import "HXPhotoPicker.h"

@interface HEAddPhotoCell ()<HXPhotoViewDelegate>
 

/**  照片视图  */
@property (nonatomic, strong) HXPhotoView *photoView;

/**  照片管理  */
@property (nonatomic, strong) HXPhotoManager *manager;

@property (nonatomic, assign) CGFloat cellHeight;

@property (nonatomic, strong) UITableView *tableView;

@property (copy, nonatomic) NSArray *selectList;

@property (nonatomic, assign) BOOL hasCreate;

@end

@implementation HEAddPhotoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;

        [self initUI];
    }
    return self;
}

- (void)initUI {
 
    [self addSubview:self.photoView];
    [self.photoView refreshView];
}

- (void)refreshContent:(GDFiterCellModel *)fiterModel formModel:(GDFormModel *)formModel indexPath:(NSIndexPath *)indexPath {
    
    [self setupData];
 

    self.fiterModel.cellHeight = _cellHeight;
    [self refreshCellHeight];
 
    [self.manager changeAfterSelectedArray: self.formModel.cm_hximgs];
}


- (void)setupData {
    
    _photoView.lineCount = self.fiterModel.lineNum;
    _manager.configuration.maxNum = self.fiterModel.maxImgNum;

    switch (self.fiterModel.photoMangeType) {
        case GDCellPhotoManagerSelectedTypePhoto:
        {
            _manager.type = HXPhotoManagerSelectedTypePhoto;
            _manager.configuration.selectTogether = NO;
            _manager.configuration.photoMaxNum = self.fiterModel.maxImgNum;
            _manager.configuration.videoMaxNum = 0;
        }
            break;
        case GDCellPhotoManagerSelectedTypePhotoAndVideo:
        {
            _manager.type = HXPhotoManagerSelectedTypePhotoAndVideo;
            _manager.configuration.selectTogether = YES;
            _manager.configuration.photoMaxNum = 0;
            _manager.configuration.videoMaxNum = 0;
        }
            break;
        case GDCellPhotoManagerSelectedTypeVideo:
        {
            _manager.type = HXPhotoManagerSelectedTypeVideo;
            _manager.configuration.selectTogether = NO;
            _manager.configuration.photoMaxNum = 0;
            _manager.configuration.videoMaxNum = self.fiterModel.maxImgNum;
        }
            break;
        default:
            break;
    }
}


- (void)photoView:(HXPhotoView *)photoView changeComplete:(NSArray<HXPhotoModel *> *)allList photos:(NSArray<HXPhotoModel *> *)photos videos:(NSArray<HXPhotoModel *> *)videos original:(BOOL)isOriginal {
    self.selectList = allList;
    if (photoView == self.photoView) {
        self.formModel.cm_hximgs = self.manager.afterSelectedArray.mutableCopy;
        [self getImages: photos];
        [self getVideo:videos];
    }
}

- (void)getImages:(NSArray<HXPhotoModel *> *)photos {
    self.formModel.cm_imgs = @[].mutableCopy;
    CGSize size = PHImageManagerMaximumSize;
    for (HXPhotoModel *item in photos) {
        [item requestPreviewImageWithSize:size startRequestICloud:^(PHImageRequestID iCloudRequestId, HXPhotoModel * _Nullable model) {
            // 如果图片是在iCloud上的话会先走这个方法再去下载
        } progressHandler:^(double progress, HXPhotoModel * _Nullable model) {
            // iCloud的下载进度
        } success:^(UIImage * _Nullable image, HXPhotoModel * _Nullable model, NSDictionary * _Nullable info) {
            // image
            [self.formModel.cm_imgs addObject: image];

        } failed:^(NSDictionary * _Nullable info, HXPhotoModel * _Nullable model) {
            // 获取失败
        }];
    }
}

- (void)getVideo:(NSArray<HXPhotoModel *> *)videos {
    self.formModel.cm_video = @[].mutableCopy;
    for (HXPhotoModel *model in videos) {
        // HXPhotoModel也提供直接导出视频地址的方法
        // presetName 导出视频的质量，自己根据需求设置
        [model exportVideoWithPresetName:AVAssetExportPresetMediumQuality startRequestICloud:nil iCloudProgressHandler:nil exportProgressHandler:^(float progress, HXPhotoModel * _Nullable model) {
            // 导出视频时的进度，在iCloud下载完成之后
            
        } success:^(NSURL * _Nullable videoURL, HXPhotoModel * _Nullable model) {
            // 导出完成, videoURL
            [self.formModel.cm_video addObject:videoURL];
        } failed:nil];
    }
}

- (void)photoView:(HXPhotoView *)photoView updateFrame:(CGRect)frame {
    _cellHeight = frame.size.height + self.fiterModel.spacing + 20;
    self.fiterModel.cellHeight = _cellHeight;
    [self refreshCellHeight];
}

- (void)refreshCellHeight {
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

- (HXPhotoManager *)manager {
    if (!_manager) {
        _manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhoto];
        _manager.configuration.openCamera = YES;
        _manager.configuration.photoMaxNum = 9;
        _manager.configuration.videoMaxNum = 0;
        _manager.configuration.maxNum = 9;
        _manager.configuration.lookLivePhoto = YES;
        _manager.configuration.selectTogether = NO;
        _manager.configuration.deleteTemporaryPhoto = YES;
        _manager.configuration.creationDateSort = YES;
    }
    return _manager;
}

- (HXPhotoView *)photoView {
    if (!_photoView) {
        _photoView = [[HXPhotoView alloc] initWithFrame:CGRectMake(15, 15, KScreenWidth - 30, 0) manager:self.manager];
        _photoView.delegate = self;
        _photoView.addImageName = @"ic_add_image";
        _photoView.previewStyle = HXPhotoViewPreViewShowStyleDark;
        _photoView.spacing = 10;
    }
    return _photoView;
}

- (UITableView *)tableView {
    if (!_tableView) {
        // 寻找Cell所属的tableView
        UIView *tableView = self.superview;
        while (![tableView isKindOfClass:[UITableView class]] && tableView) {
            tableView = tableView.superview;
        }
        _tableView = (UITableView *)tableView;
    }
    return _tableView;
}

@end
